#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class MyString
/**
 * @brief
 * 无参构造
 * 有参构造
 * 拷贝构造
 * 析构
 * 获取字符串长度
 * []重载
 * +重载
 * 赋值运算符重载
 * 判断大小长度 >重载
 * << 输出重载
 */
{
    friend ostream &operator<<(ostream &out, MyString ob);
    friend istream &operator>>(istream &in, MyString &ob);

private:
    const char *str;
    int size;

public:
    MyString();
    MyString(const char *string);
    MyString(const MyString &ob); // 默认的拷贝构造都是浅拷贝
    // ~MyString();
    char operator[](int pos);

    MyString operator+(MyString ob);
    MyString operator+(char *str);

    MyString &operator=(MyString &ob);
    MyString &operator=(char &str);

    bool operator>(MyString &str);
    bool operator>(char *str);

    int get_str_length();
    void print()
    {
        for (size_t i = 0; str[i] != '\0'; i++)
        {
            cout << str[i] << endl;
        }
    }
};

bool MyString::operator>(MyString &ob)
{
    if (this->size > ob.size)
    {
        return true;
    }
    return false;
}
bool MyString::operator>(char *str)
{
    if (this->size > strlen(str))
    {
        return true;
    }
    return false;
}
MyString MyString::operator+(MyString ob)
{
    this->size = this->size + ob.size;
    char *tem = new char[this->size + 1];
    memset(tem, 0, this->size + 1);
    strcpy(tem, this->str);
    strcat(tem, ob.str);
    this->str = tem;
    return *this;
}
MyString MyString::operator+(char *str)
{
    this->size = this->size + strlen(str);
    char *tem = new char[this->size + 1];
    memset(tem, 0, this->size + 1);
    strcpy(tem, this->str);
    strcat(tem, str);
    this->str = tem;
    return *this;
}
ostream &operator<<(ostream &out, MyString ob) //不要取引用，因为这里是输出，而不是修改
{
    out << ob.str;
    return out;
}
istream &operator>>(istream &in, MyString &ob)
{
    char tem[123];
    in >> tem;
    char *temp = new char[strlen(tem) + 1];
    memset(temp, 0, strlen(tem) + 1);
    strcpy(temp, tem);
    ob.str = temp;
    temp = NULL;
    return in;
}
MyString::MyString()
{
    str = NULL;
    size = 0;
}

MyString::MyString(const char *string)
{
    str = string;
    size = strlen(string);
    // this->str = new char[strlen(str) + 1];
    // memset(this->str, 0, strlen(str) + 1);
    // size = strlen(str);
    // strcpy(this->str, str);
}

MyString::MyString(const MyString &ob)
{
    /**
     * @brief
     * 通过一个临时指针进行赋值操作
     */
    char *tem_str = new char[strlen(ob.str) + 1];
    memset(tem_str, 0, strlen(ob.str) + 1);
    size = strlen(ob.str);
    strcpy(tem_str, ob.str);
    str = tem_str;
    tem_str = NULL; //临时指针置空
}
// MyString::~MyString()
// {
//     if (str != NULL)
//     {
//         delete str;
//         str = NULL;
//     }
// }
int MyString::get_str_length()
{
    return strlen(str);
}

int main()
{
    MyString str = "hello";
    cout
        << str
        << endl;
    MyString temp = "world";
    MyString tt = str + temp;
    cout << tt << endl;
    const char *s = "hihi";
    cout << tt + s << endl;
    cout << tt[3] << endl;
    if (tt > str)
    {
        cout << "大于";
    }
    else
        cout << "小于";
}

char MyString::operator[](int pos)
{
    if (pos < 0 || pos > this->size)
    {
        cout << "下标越界";
        exit(-1);
    }
    return this->str[pos];
}